<?php

namespace app\api\service;


use Exception;
use think\Db;

class ToolService
{
    //获取城市经纬度 //腾讯
    public static function getlongitude($address)
    {
        $url = 'https://apis.map.qq.com/ws/geocoder/v1/?address=' . $address . '&key=WBZBZ-525HV-NQKP6-UXVVD-3PAOK-CVBAI';
        try {
            $data = json_decode(file_get_contents($url), true);
            $and = [
                'lat' => $data['result']['location']['lat'],
                'lng' => $data['result']['location']['lng']
            ];
            $tou = implode(',', $and);
            return $tou;
        } catch (Exception $e) {
            return false;
        }
    }

    //获取公里数 //腾讯
    public static function getmileage($origin, $terminus)
    {
        $zongurl = "https://apis.map.qq.com/ws/direction/v1/walking/?from=" . $origin . "&to=$terminus&output=json&callback=cb&key=WBZBZ-525HV-NQKP6-UXVVD-3PAOK-CVBAI";
        try {
            $data = json_decode(file_get_contents($zongurl), true);
            $num = $data['result']['routes'][0]['distance'] / 1000;
            return $num;
        } catch (Exception $e) {
            return false;
        }
    }

    /**
     * 根据起点坐标和终点坐标测距离
     * @param  [string]   $from    [起点坐标(经纬度),例如:36.810024,118.012951
     * @param  [string]   $to    [终点坐标(经纬度)]
     * @param  [bool]    $km        是否以公里为单位 false:米 true:公里(千米)
     * @param  [int]     $decimal   精度 保留小数位数
     * @return [string]  距离数值
     */
    public static function distance1($from, $to, $km = true, $decimal = 2)
    {
        $from = explode(',', $from);
        $to = explode(',', $to);
//        sort($from);
//        sort($to);
        $EARTH_RADIUS = 6370.996; // 地球半径系数
        $distance = $EARTH_RADIUS * 2 * asin(sqrt(pow(sin(($from[1] * pi() / 180 - $to[1] * pi() / 180) / 2), 2) + cos($from[1] * pi() / 180) * cos($to[1] * pi() / 180) * pow(sin(($from[0] * pi() / 180 - $to[0] * pi() / 180) / 2), 2))) * 1000;
        if ($km) {
            $distance = $distance / 1000;
        }
        return round($distance, $decimal);
    }

    /**
     *求两个已知经纬度之间的距离,单位为千米
     * @param lng1,lng2 经度
     * @param lat1,lat2 纬度
     * @return float 距离，单位千米
     **/
    public static function distance($lng1, $lat1, $lng2, $lat2)//根据经纬度计算距离
    {
        //将角度转为弧度
        $radLat1 = deg2rad((float)$lat1);
        $radLat2 = deg2rad((float)$lat2);
        $radLng1 = deg2rad((float)$lng1);
        $radLng2 = deg2rad((float)$lng2);
        $a = $radLat1 - $radLat2;//两纬度之差,纬度<90
        $b = $radLng1 - $radLng2;//两经度之差纬度<180
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137;
        return $s;
    }

    //获取城市经纬度 //百度
    public function getlongitudes($address)
    {
        $url = 'http://api.map.baidu.com/geocoding/v3/?address=' . $address . '&output=json&ak=L3Ls0xBfwxx7B9Wcrkf5fxCMpcIuDtI6';

        $data = json_decode(file_get_contents($url), true);
        $and = [
            'lat' => $data['result']['location']['lat'],
            'lng' => $data['result']['location']['lng']
        ];
        $tou = implode(',', $and);
        return $tou;
    }


    //获取公里数  //百度
    function getmileages($origin, $terminus)
    {
        $zongurl = 'http://api.map.baidu.com/directionlite/v1/driving?origin=' . $origin . '&destination=' . $terminus . '&ak=L3Ls0xBfwxx7B9Wcrkf5fxCMpcIuDtI6';
        $data = json_decode(file_get_contents($zongurl), true);
        $num = $data['result']['routes'][0]['distance'] / 1000;
        return $num;
    }


    public static   function savesearch($user_id,$params)
    {
        $data  = DB::table('hs_search_history')->where('user_id', $user_id)->where('search_query', $params['name'])->find();
        if(!$data){
           DB::table('hs_search_history')->insert(['user_id'=>$user_id,'search_query'=>$params['name']]);
        }
    }

}